<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use app\common\model\Users;
use think\Session;
use think\captcha\Captcha;

class Login extends Controller
{
    /*登录页面*/
    public function index(Request $request)
    {
        //登录请求
        if ($request->isPost()) {
            $data = input('post.');
            if ($data['randnum'] != session('randnum')) {
                $this->error('请不要攻击本站，谢谢');
                exit;
            }

            if (!captcha_check($data['code'])) {
                $this->error('验证码错误');
            };
            $res = Users::where('username', $data['username'])->find()->toArray();
            if (!$res || $res['password'] != md5($data['password'])) {
                $this->error('输入的用户名不存在或密码错误');
            }
            $adminData['id'] = $res['id'];
            $adminData['username'] = $res['username'];
            //记录session
            Session::set('user', $adminData);
            $this->success('登录成功', 'Index/index');
            exit();
        };
        $randnum = rand(11111, 9999999);
        session('randnum', $randnum);
        return view('', compact('randnum'));
    }


    /*退出登录*/
    public function outlogin()
    {
        session(null);
        $this->success('退出成功', url('index'));

    }

    /*验证码*/
    public function codecreate()
    {
        $config = [
            // 验证码位数
            'length' => 1,
            // 验证码字体大小(px)
            'fontSize' => 26,
            // 关闭验证码杂点
            'useNoise' => false,
            // 是否画混淆曲线
            'useCurve' => false,
            // 验证码图片高度
            'imageH' => 60,
            // 验证码图片宽度
            'imageW' => 200,
            // 验证成功后是否重置
            'reset' => true
        ];

        $captcha = new Captcha($config);
        return $captcha->entry();
    }

    /*模拟邮件*/
    public function sendMail()
    {
        $callback = input('callback');
        $data['status'] = 1;
        $data['msg'] = '发送成功';
        $data['email'] = input('email');
        $data = json_encode($data);
        return $callback . '(' . $data . ')';  //返回格式，必需
    }

}
